<template>
  <view>
    <!-- 筛选弹窗 -->
    <view class="filter-mask" :class="{ show: show }" @click="closeFilter">
      <view class="filter-popup" @click.stop>
        <!-- 弹窗头部 -->
        <view class="filter-header">
          <view class="filter-title">筛选</view>
          <view class="filter-close" @click="closeFilter">
            <text>×</text>
          </view>
        </view>
        
        <!-- 时间选择区域 -->
        <view class="filter-section">
          <view class="time-picker">
			  <view class="time-flex">
				  <text>开始时间：</text>
				  <view class="time-input" @click="pickStartTime">
				    <LeeDatetime @change="getStartTime"></LeeDatetime>
				  </view>
			  </view>
            
			
			<view class="time-flex">
				<text>结束时间：</text>
				<view class="time-input" @click="pickEndTime">
				  <LeeDatetime @change="getEndTime"></LeeDatetime>
				</view>
			</view>
            
          </view>
        </view>
        
        <!-- 订单类型选择 -->
        <view class="filter-section">
          <view class="section-title">{{onetitle}}</view>
          <view class="filter-options">
            <view 
              v-for="index in orderTypes" 
              :key="index.value"
              class="option-item" 
              :class="{ selected: selectedOrderTypes.includes(index.value) }"
              @click="toggleOrderType(index.value)"
            >
			<image class="filter-image" :src="index.img"></image>
              <text>{{ index.label }}</text>
            </view>
          </view>
        </view>
        
        <!-- 订单状态选择 -->
		<view class="filter-section">
		  <view class="section-title">{{twotitle}}</view>
		  <view class="filter-options">
		    <view
		      v-for="status in orderStatus" 
		      :key="status.value"
		      class="option-item"
		      :class="{ selected: selectedOrderStatus.includes(status.value) }"
		      @click="toggleOrderStatus(status.value)"
		    >
		      <text>{{ status.label }}</text>
		    </view>
		  </view>
		</view>
        
        <!-- 底部按钮 -->
        <view class="filter-footer">
          <button class="footer-btn btn-cancel" @click="resetFilter">重置</button>
          <button class="footer-btn btn-confirm" @click="applyFilter">应用筛选</button>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import LeeDatetimeVue from '../LeeDatetime/LeeDatetime.vue';
export default {
	components: { LeeDatetimeVue },
  props: {
    show: {
      type: Boolean,
      default: false
    },
	onetitle:{
		type:String,
		default:"订单类型"
	},
	twotitle:{
		type:String,
		default:"订单状态"
	},
    filterData: {
      type: Object,
      default: () => ({
        startTime: '',
        endTime: '',
        orderTypes: [],
        orderStatus: []
      })
    }
  },
  data() {
    return {
      orderTypes: [
        { label: '银行卡', value: 'bank',img:'/static/payImg/yinghangkazhuangzhang.png' },
        { label: '微信', value: 'wechat',img:'/static/payImg/weixin.png'},
        { label: '支付宝', value: 'alipay',img:'/static/payImg/zhifubao.png' },
        { label: '云闪付', value: 'unionpay',img:'/static/payImg/yunshanfu.png' },
        { label: '数字人民币', value: 'digital',img:'/static/payImg/shuzirenmingbi.png' },
        { label: '钉钉', value: 'dingtalk',img:'/static/payImg/dingding.png' }
      ],
      orderStatus: [
        { label: '待支付', value: 'pending' },
        { label: '待确认', value: 'confirmed' },
        { label: '已完成', value: 'completed' },
        { label: '暂停中', value: 'paused' },
        { label: '已取消', value: 'canceled' }
      ]
    };
  },
  computed: {
    // 使用计算属性确保安全访问
    selectedOrderTypes() {
      return this.filterData.orderTypes || [];
    },
    selectedOrderStatus() {
      return this.filterData.orderStatus || [];
    }
  },
  methods: {
    closeFilter() {
      this.$emit('close');
    },
	//开始时间点击事件，向父组件传递事件
    pickStartTime() {
      this.$emit('pick-time', 'start');
    },
	//结束时间点击事件，向父组件传递事件
    pickEndTime() {
      this.$emit('pick-time', 'end');
    },
	//获取日期组件选中开始时间
	getStartTime(e){
		console.log('获得时间',e)
	},
	//获取日期组件选中结束时间
	getEndTime(e){
		console.log('获得结束时间',e)
	},
    toggleOrderType(type) {
      // 创建新数组避免直接修改props
      const newOrderTypes = [...this.selectedOrderTypes];
      const index = newOrderTypes.indexOf(type);
      
      if (index > -1) {
        newOrderTypes.splice(index, 1);
      } else {
        newOrderTypes.push(type);
      }
      
      // 触发事件更新父组件数据
      this.$emit('update-order-types', newOrderTypes);
    },
    toggleOrderStatus(status) {
      // 创建新数组避免直接修改props
      const newOrderStatus = [...this.selectedOrderStatus];
      const index = newOrderStatus.indexOf(status);
      
      if (index > -1) {
        newOrderStatus.splice(index, 1);
      } else {
        newOrderStatus.push(status);
      }
      
      // 触发事件更新父组件数据
      this.$emit('update-order-status', newOrderStatus);
    },
    resetFilter() {
      this.$emit('reset');
    },
    applyFilter() {
      this.$emit('confirm', this.filterData);
      this.closeFilter();
    }
  }
};
</script>

<style scoped>
/* 筛选弹窗样式 - 使用rpx单位确保多端适配 */
.filter-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 900;
  display: flex;
  justify-content: center;
  align-items: flex-end;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
}

.filter-mask.show {
  opacity: 1;
  pointer-events: auto;
}

.filter-popup {
  background: white;
  width: 100%;
  border-radius: 24rpx 24rpx 0 0;
  box-shadow: 0 -10rpx 30rpx rgba(0, 0, 0, 0.15);
  transform: translateY(100%);
  transition: transform 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.1);
  overflow: hidden;
  padding: 0 30rpx 30rpx;
  max-height: 80vh; /* 限制最大高度 */
  overflow-y: auto; /* 允许内容滚动 */
}

.filter-mask.show .filter-popup {
  transform: translateY(0);
}

.filter-header {
  padding: 40rpx 0 30rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: sticky;
  top: 0;
  background: white;
  z-index: 10;
  border-bottom: 1rpx solid #e0e0e0;
}

.filter-title {
  font-size: 36rpx;
  font-weight: 600;
  color: #2c3e50;
}

.filter-close {
  width: 60rpx;
  height: 60rpx;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f5f7fa;
  color: #7f8c8d;
  font-size: 36rpx;
}

.filter-section {
  padding: 0rpx 0 30rpx 0;
  /* border-bottom: 1rpx solid #f0f0f0; */
  
}
.filter-image{
	width: 35rpx;
	height: 35rpx;
	padding-right: 5rpx;
}
.section-title {
  font-size: 32rpx;
  font-weight: 500;
  color: #555;
  margin-bottom: 20rpx;
}

.time-picker {
  display: flex;
  flex-direction: row;
  align-items: center;
  /* gap: 20rpx; */
}

.time-input {
  flex: 1;
  min-width: 300rpx;
  height: 80rpx;
  /* border: 1rpx solid #e0e0e0; */
  /* border-radius: 16rpx; */
  padding: 0 30rpx;
  font-size: 28rpx;
  color: #333;
  /* background: #fafafa; */
  display: flex;
  align-items: center;
}
.time-flex{
	display: flex;
	align-items: center;
	border-bottom: 1rpx solid #e0e0e0;
	padding: 15rpx 0;
}
.filter-options {
  display: flex;
  flex-wrap: wrap; /* 允许换行 */
  justify-content: flex-start; /* 从左对齐 */
  gap: 20rpx; /* 元素间距 */
}

.option-item {
  box-sizing: border-box; /* 包含padding/border */
  flex: 0 0 calc((100% - 40rpx) / 3); /* 关键：三列布局 */
  padding: 20rpx 30rpx;
  background: #f5f7fa;
  border-radius: 16rpx;
  font-size: 28rpx;
  color: #555;
  transition: all 0.2s ease;
  border: 1rpx solid transparent;
  /* text-align: center; */
  display: flex;
  align-items: center;
  justify-content: center;
}

.option-item.selected {
  background: #e1f0ff;
  color: #3498db;
  border-color: #3498db;
  font-weight: 500;
  box-shadow: 0 8rpx 20rpx rgba(52, 152, 219, 0.15);
}

.filter-footer {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  gap: 20rpx;
  padding: 40rpx 0 20rpx;
}

.footer-btn {
  flex: 1;
  padding: 25rpx;
  border-radius: 16rpx;
  font-size: 32rpx;
  font-weight: 500;
  border: none;
  transition: all 0.2s ease;
}

.btn-cancel {
  background: #f5f7fa;
  color: #7f8c8d;
}

.btn-confirm {
  background: linear-gradient(135deg, #3498db 0%, #2980b9 100%);
  color: white;
  box-shadow: 0 8rpx 20rpx rgba(52, 152, 219, 0.3);
}

/* 响应式适配 */
@media (max-width: 750px) {
  .filter-popup {
    padding: 0 20rpx 20rpx;
  }
  
  .time-picker {
    flex-direction: column;
    align-items: stretch;
  }
  
 
  .option-item {
    padding: 15rpx 20rpx;
    font-size: 26rpx;
    min-width: 120rpx;
  }
  
  .footer-btn {
    padding: 20rpx;
    font-size: 28rpx;
  }
}
</style>